#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "admin.h"

// 初始化管理员信息
struct admin* admin_inite()
{
    struct admin* admin = malloc(sizeof(struct admin));
    // 正确初始化管理员账户
    strcpy(admin->username, "admin"); 
    strcpy(admin->password, "123456"); 
    admin->next = NULL;
    return admin;
}

// 管理员登录 - 修复参数顺序
int admin_login(struct admin* list, char* username, char* password)
{
    int flag = 0;
    struct admin* p = list;
    // 遍历链表查找用户名和密码
    while (p != NULL)
    {
        if (strcmp(p->username, username) == 0 && strcmp(p->password, password) == 0)
        {
            printf("管理员登录成功\n");
            flag = 1;
            return flag; // 登录成功
        }
        p = p->next;
    }
    printf("管理员用户名或密码错误\n");
    return 0; // 登录失败
}

// 添加缺失的函数实现
void show_all_users(struct user* user_list) {
    struct user* p = user_list->next; // 跳过头节点
    printf("---所有用户信息---\n");
    while(p != NULL) {
        printf("用户名: %s\t", p->username);
        printf("黑名单: %s\t", p->is_blacklisted ? "是" : "否");
        printf("白名单: %s\n", p->is_whitelisted ? "是" : "否");
        p = p->next;
    }
}

void add_flight(struct flight* flight_list) {
    char flight_number[10];
    float start_time, finish_time;
    char location[30], destination[30];
    int price;
    
    printf("请输入航班号: ");
    scanf("%s", flight_number);
    printf("请输入起飞时间: ");
    scanf("%f", &start_time);
    printf("请输入到达时间: ");
    scanf("%f", &finish_time);
    printf("请输入起点: ");
    scanf("%s", location);
    printf("请输入终点: ");
    scanf("%s", destination);
    printf("请输入价格: ");
    scanf("%d", &price);
    
    insert_flight(flight_number, start_time, finish_time, location, destination, price, flight_list);
    printf("航班添加成功！\n");
     // 立即保存到文件
    save_flights_to_file(flight_list, "flights.dat");
    printf("航班信息已保存到文件\n");

}

void modify_flight(struct flight* flight_list) {
    char flight_number[10];
    printf("请输入要修改的航班号: ");
    scanf("%s", flight_number);
    
    struct flight* p = flight_list->next;
    while(p != NULL) {
        if(strcmp(p->flight_number, flight_number) == 0) {
            printf("找到航班，当前信息:\n");
            printf("航班号: %s\n", p->flight_number);
            printf("起飞时间: %.2f\n", p->strattime);
            printf("到达时间: %.2f\n", p->finishtime);
            printf("起点: %s\n", p->location);
            printf("终点: %s\n", p->destination);
            printf("价格: %d\n", p->price);
            
            printf("请输入新的起飞时间: ");
            scanf("%f", &p->strattime);
            printf("请输入新的到达时间: ");
            scanf("%f", &p->finishtime);
            printf("请输入新的价格: ");
            scanf("%d", &p->price);
            
            printf("航班信息修改成功！\n");
            return;
        }
        p = p->next;
    }
    printf("未找到航班号为 %s 的航班\n", flight_number);
}

int query_flight(struct flight* flight_list) {
    int choice;
    printf("请选择查询方式:\n");
    printf("1. 按目的地查询\n");
    printf("2. 按出发地查询\n");
    printf("请选择: ");
    scanf("%d", &choice);
    
    char location[30];
    switch(choice) {
        case 1:
            printf("请输入目的地: ");
            scanf("%s", location);
            return query_flight_by_destination(location, flight_list);
        case 2:
            printf("请输入出发地: ");
            scanf("%s", location);
            return query_flight_by_location(location, flight_list);
        default:
            printf("无效选择\n");
            return 0;
    }
}

void free_admin(struct admin* list) {
    struct admin* p = list;
    while(p != NULL) {
        struct admin* temp = p;
        p = p->next;
        free(temp);
    }
}

